class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        if (!root) return ans;
        stack<TreeNode*> st;
        st.push(root);
        while (!st.empty())
        {
            TreeNode* top = st.top();
            st.pop();

            if (top->left) st.push(top->left);
            if (top->right) st.push(top->right);
            ans.push_back(top->val);
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};